//
//  LYDatabase+Migrate.m
//  Apps
//
//  Created by 李永彬 on 2019/6/5.
//  Copyright © 2019 appscomm. All rights reserved.
//

#import "LYDatabase+Migrate.h"
#import "NSObject+LYDatabase.h"

@implementation LYDatabase (Migrate)

- (BOOL)needUpdateTableArray:(NSArray <Class>*)array {
    for (Class class in array) {
        NSString *tableName = [class tableName];
        if(![self isTableExist:tableName]) {
            return YES;
        }
    }
    return NO;
}

- (NSArray <Class>*)needUpdateTable:(NSArray <Class>*)array {
    
    NSMutableArray *mut = [NSMutableArray array];
    for (Class class in array) {
        NSString *tableName = [class tableName];
        if (![self isTableExist:tableName]) {
            [mut addObject:class];
        }
    }
    return [mut copy];
}

/**
//第一种获取字段名方法
- (NSArray *)columnsWithTable:(NSString *)tableName {
    //数据库中现有的字段
    NSMutableArray *columnsArray = [NSMutableArray array];
    NSString *sql = [NSString stringWithFormat:@"select * from %@",tableName] ;
    FMResultSet *rs = [self.db executeQuery:sql];
    NSDictionary *dict = [rs columnNameToIndexMap];
    [columnsArray addObjectsFromArray:[dict allKeys]];
    return columnsArray;
}
*/

//第二种获取字段名方法
- (NSArray *)columnsWithTable:(NSString *)tableName {
    __block NSMutableArray <NSString*>*tableColumes = [NSMutableArray array];
    [self inDatabase:^(FMDatabase *db) {
        NSString* sql = [NSString stringWithFormat:@"PRAGMA table_info('%@')", tableName];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString* columnName = [rs stringForColumn:@"name"];
            [tableColumes addObject:columnName];
        }
    }];
    return tableColumes;
}

- (NSArray*)sqliteExistsTables {
    __block NSMutableArray<NSString*>* existsTables = [NSMutableArray array];
    [self inDatabase:^(FMDatabase *db) {
        NSString* sql = @"SELECT * from sqlite_master WHERE type='table'";
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString* tablename = [rs stringForColumn:@"name"];
            [existsTables addObject:tablename];
        }
    }];
    return existsTables;
}

- (NSInteger)selectWithTableName:(NSString *)tableName
                           column:(NSString *)column
{
 
    __block NSInteger dbVersion = 0;
    [self inDatabase:^(FMDatabase *db) {
        
        NSString *sql = [NSString stringWithFormat:@"select * from %@", tableName];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {

            dbVersion = [rs intForColumn:column];
        }
        [rs close];
       
    }];
    
    return dbVersion;
}

- (void)createIfNotExistTable:(NSString *)table version:(NSInteger)version column:(NSString *)column {
    if (![self isTableExist:table]) {
        NSString *createSql = [NSString stringWithFormat:@"create table if not exists %@ (%@ integer primary key not null);", table, column];
        BOOL result = [self createTable:createSql];
        // 生成插入数据库语句
        NSString *insertSql = [NSString stringWithFormat:@"insert into %@ (%@) values(?)", table, column];
        BOOL res = [self executeUpdate:insertSql, @(version)];
    }
}

- (void)updateVersion:(NSInteger)version withTableName:(NSString *)tableName column:(NSString *)column {
    NSString *updateSql = [NSString stringWithFormat:@"update %@ set %@ = ?", tableName, column];
    BOOL res = [self executeUpdate:updateSql, @(version)];
}

- (void)createToInsertOrUpdateTableName:(NSString *)tableName
                                column:(NSString *)column
                               version:(NSInteger)version
{
    
    if (![self isTableExist:tableName]) {
        NSString *createSql = [NSString stringWithFormat:@"create table if not exists %@ (%@ integer primary key not null);", tableName, column];
        BOOL result = [self createTable:createSql];
        // 生成插入数据库语句
        NSString *insertSql = [NSString stringWithFormat:@"insert into %@ (%@) values(?)", tableName, column];
        BOOL res = [self executeUpdate:insertSql, version];
        
    }else {
        NSString *updateSql = [NSString stringWithFormat:@"update %@ set %@ = ?", tableName, column];
        BOOL res = [self executeUpdate:updateSql, version];
    }
}

@end
